package com.qfjy.service.impl;


import com.qfjy.entity.po.MeetingPub;
import com.qfjy.mapper.MeetingPubMapper;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName MeetingPubServiceImpl
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/12/5
 * @Version 1.0
 */
@Service
@Slf4j
public class MeetingPubServiceImpl implements MeetingPubService {

    @Autowired
    private MeetingPubMapper meetingPubMapper;
    /**
     * TODO 添加会议
     *
     * @param meetingPub
     * @return
     */
    @Override
    public int insertSelective(MeetingPub meetingPub) {

        meetingPub.setId(UUID.randomUUID().toString());
        meetingPub.setStatus((short) 1);
        meetingPub.setCreateDate(new Date());

        //会议编号 自定义生成规则（分布式ID）
       meetingPub.setPcode(this.meetingPubPcodeGenerator(meetingPub.getPTime()));

        return meetingPubMapper.insertSelective(meetingPub);
    }

    @Override
    public List<MeetingPub> selectMeetingPubByUid(int uid) {
        return meetingPubMapper.selectMeetingPubByUid(uid);
    }

    /**
     * TODO 会议抢单 可抢单列表
     *
     * @param uid 抢单人uid
     * @param tid 课题类别主键ID
     * @return
     */
    @Override
    public List<MeetingPub> grabListByUid(int uid, int tid) {
        return meetingPubMapper.grabListByUid(uid,tid);
    }

    /**
     * TODO 会议抢单-》可抢单者列表->我的可抢单列表
     * 抢单人抢了哪些单子列表
     *
     * @param uid 抢单人uid
     */
    @Override
    public List<MeetingPub> grabListMyByUid(int uid) {
        return meetingPubMapper.grabListMyByUid(uid);
    }


    /**
     * 会议编号 生成规则
     *   根据会议召开日期 来判断 是否存在会议信息，依次按照规则进行+1，如果是首单 001拼接
     *   2022-12-10T10:24
     *   根据ptime 查询  这个日期的最大的会议编号， select max(pcode) from meeting_pub  where p_time like '2022-12-11%';
     *   select max(pcode) from meeting_pub  where LEFT(p_time,10) ='2022-12-11'
     *    判断返回值，如果为NULL，代表首单001
     *    如果不为NULL， +1操作
     *
     */
    public String meetingPubPcodeGenerator(String ptime){


        String time= ptime.substring(0,10);
        String str=time.replaceAll("-","");
        String result=meetingPubMapper.selectMeetingPubMaxPcodeByPtime(time);

        StringBuffer stringBuffer=new StringBuffer(str);


        if(StringUtils.isEmpty(result)){
            return  stringBuffer.append("001").toString();
        }else{
            long num=Long.parseLong(result);
                num=num+1;
                return  Long.toString(num);
        }

    }


}

